// pages/login/login.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag1:0, // 判断用户是否受到了验证短信
    flag2:0, // 判断用户是否接收到了验证码
    flag3:0, // 判断用户是否点击了完成注册
    verifyCode: 'CSC123', // 验证码
    verifyCodeUserPut: '', // 用户输入的验证码 
    Token1: '', // 华为云鉴权
    subscription_urn:'', // 消息推送服务用户的唯一标识
    phoneInputValue: '', // 手机号
    passwordInputValue:'', // 密码 
    nameInputValue:'',
  },
  // 用户输入手机号
  phoneInput: function (e) {
    this.setData({
      phoneInputValue: e.detail.value
    })
    console.log(this.data.phoneInputValue)
  },
  // 用户输入密码
  passwordInput(e){
    this.setData({
      passwordInputValue:e.detail.value
    })
    console.log(this.data.passwordInputValue)
  },
  // 用户输入昵称
  nameInput(e){
    this.setData({
      nameInputValue: e.detail.value
    })
    console.log(this.data.nameInputValue)
  },
    // 用户输入验证码
    verifyCodeInput(e){
      this.setData({
        verifyCodeUserPut:e.detail.value
      })
    },
  // 用户点击注册
  register(e){
    const regex = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
    if(this.data.phoneInputValue.length != 11 && this.data.phoneInputValue != 0){
      wx.showToast({
        title: '手机号长度有误',
        icon:'error',
        duration:1000
      })
      return
    }
    else if(this.data.phoneInputValue.length !== 0 && !regex .test(this.data.phoneInputValue)){
      wx.showToast({
        title: '您的手机号有误',
        icon:'error',
        duration:1000
      })
    }
    else if(this.data.flag1 == 1){
      wx.showToast({
        title: '请不要重复点击',
        icon:'error',
      })
    }
    else{
      // verifySMS()
      this.verifySMS()
    }
  },
  // 用户点击完成注册
  completeRegister(){
    // 验证码不对时候
    if(this.data.verifyCodeUserPut != this.data.verifyCode){
      wx.showToast({
        title: '验证码错误',
        icon:'error',
      })
    }
    else if(this.data.flag2 == 1 & this.data.flag3 == 0){
      wx.request({
        method:'POST',
        url: app.globalData.serverPort+'/user/save',
        data:{
          "pass_word": this.data.passwordInputValue,
          "phone_num": this.data.phoneInputValue,
          "name": this.data.nameInputValue
        },
        // header:{
        //   'Content-Type': 'application/json'
        // }
        success:res=>{
          wx.showToast({
            title: '注册成功',
            duration:1000
          })
          setTimeout(function () {
            // 延时1秒后执行下一个操作
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }, 1000);
          // wx.navigateTo({
          //   url: '/pages/login/login'
          // })
        }
      })
      // 失败的问题在于this.data.urn没有切割好，
      wx.request({
        method:'DELETE',
        url: 'https://smn.cn-north-4.myhuaweicloud.com/v2/a823246b24884e77ad5b11c462d32122/notifications/subscriptions/urn%3Asmn%3Acn-north-4%3Aa823246b24884e77ad5b11c462d32122%3Amessage%3A'+this.data.subscription_urn,
        data:{},
        header:{
          'X-Auth-Token':app.globalData.token1
        },
        success:res=>{
          console.log("最后的urn是"+this.data.subscription_urn)
          console.log("删除的网址是" + 'https://smn.cn-north-4.myhuaweicloud.com/v2/a823246b24884e77ad5b11c462d32122/notifications/subscriptions/urn%3Asmn%3Acn-north-4%3Aa823246b24884e77ad5b11c462d32122%3Amessage%3A' + this.data.subscription_urn);
          console.log("完成了删除")
          console.log("完成注册")
          this.data.flag3 = 1;
        }
      })
    }
    else{
      wx.showToast({
        title: '不要点击了',
        duration:1000,
        icon:'error'
      })
    }
  },
  // 验证登入信息
  
  // 根据用户id获取数据
  getById(){
    wx.request({
      method: "GET",
      url: 'http://192.168.199.208:8080/user/1',
      success(res){
        console.log(res.data)
      }

    })
  },
  // 添加验证短信的用户
  verifySMS(){
    wx.request({
      method:'POST',
      url: 'https://smn.cn-north-4.myhuaweicloud.com/v2/a823246b24884e77ad5b11c462d32122/notifications/topics/urn%3Asmn%3Acn-north-4%3Aa823246b24884e77ad5b11c462d32122%3Amessage/subscriptions',
      data: {
        "protocol": "sms",
        "endpoint": this.data.phoneInputValue
      },
      header:{
        'X-Auth-Token':app.globalData.token1
      },
      success: (res)=>{
        // 发送注册链接后的提示
        wx.showToast({
          title: '已发送验证链接',
        })
        this.data.flag1 = 1
        // this.data.subscription_urn = res.subscription_urn
        console.log("用户"+this.data.phoneInputValue+"已经获取验证短信")
      }
    })
  },
  // 用户获取验证码(按钮点击)
  getVerifyCode(){
    // 已经点击验证链接和第一次发送验证短信时候才生效
    if(this.data.flag1 == 1 & this.data.flag2 == 0){
      this.getVerifyCode_1();
    }
    // 多次点击发送短信判定
    else if(this.data.flag2 == 1){
      wx.showToast({
        title: '请不要重复点击',
        icon:'error',
      })
    }
    else{
      wx.showToast({
        title: '请先获取注册短信',
        icon:'none',
        duration:1000
      })
    }
  },
  // 用户获取验证码
  getVerifyCode_1(){
    this.data.flag2 = 1
    wx.request({
      method:'POST',
      url: 'https://smn.cn-north-4.myhuaweicloud.com/v2/a823246b24884e77ad5b11c462d32122/notifications/topics/urn%3Asmn%3Acn-north-4%3Aa823246b24884e77ad5b11c462d32122%3Amessage/publish',
      data: {
        "subject": "测试1",
        "message": this.data.verifyCode
      },
      header:{
        'X-Auth-Token':app.globalData.token1 // 默认值
      },
      // 验证短信发送成功提示
      success:res=>{
        wx.showToast({
          title: '验证短信已发送',
        })
        console.log("用户已经获取验证码")
      }
    })
    wx.request({
      method:'GET',
      url: 'https://smn.cn-north-4.myhuaweicloud.com/v2/a823246b24884e77ad5b11c462d32122/notifications/topics/urn%3Asmn%3Acn-north-4%3Aa823246b24884e77ad5b11c462d32122%3Amessage/subscriptions',
      data:{

      },
      header:{
        'X-Auth-Token':app.globalData.token1 // 默认值
      },
      success:res=>{
        console.log("res是："+res.data)
        console.log("res的数量是:"+res.data.subscription_count)
        console.log("res第一个的电话:"+res.data.subscriptions[0].endpoint)
        console.log("res第一个的urn:"+res.data.subscriptions[0].subscription_urn)
        for(let i=0 ;i<res.data.subscription_count;i++){
          if(res.data.subscriptions[i].endpoint == this.data.phoneInputValue){
            console.log("我进来切割循环了")
            console.log("原先的sub："+res.data.subscriptions[i].subscription_urn)
            let str = res.data.subscriptions[i].subscription_urn;
            console.log("str:"+str)
            let lastIndex = str.lastIndexOf(":");
            console.log("lastIndex:"+lastIndex)
            let result = str.substring(lastIndex + 1);            
            console.log("result是:"+result)
            this.data.subscription_urn = result
          }
        }
        console.log("最后切割完后的urn是:"+this.data.subscription_urn)
      }
    })
  },
  // 测试跳转功能
  navigateToTest(){
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  // 测试分割功能
  deleteHwUser(){
    let strr = "asd65%bgb%3Aa651c"
    let lastIndex = strr.lastIndexOf("%")
    let result = strr.substring(lastIndex+1)
    console.log(result)
  },
  // 创建华为云消息推送的主题
  hwCreateTopic(){
    wx.request({
      method:'POST',
      url: 'https://smn.cn-north-4.myhuaweicloud.com/v2/a823246b24884e77ad5b11c462d32122/notifications/topics', //仅为示例，并非真实的接口地址
      data: {
        "name": "jianshen3",
        "display_name": "东苑健身3"
      },
      header: {
        'X-Auth-Token':app.globalData.token1 // 默认值
      },
      success (res) {
        console.log(res.data)
      }
    })
  },
  // 测试获取用户列表
  getList(){
    wx.request({
      method:'GET',
      url: 'https://smn.cn-north-4.myhuaweicloud.com/v2/a823246b24884e77ad5b11c462d32122/notifications/topics/urn%3Asmn%3Acn-north-4%3Aa823246b24884e77ad5b11c462d32122%3Amessage/subscriptions',
      header: {
        'X-Auth-Token':app.globalData.token1 // 默认值
      },
      success:res=>{
        console.log(res.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})